input: MaxFilled(5,"最大部位"),TDiff(2,"進場時間差(M)"),loss_point(10, "停損(點)");
input: FixedP(10,"固定停利模式"),MinV(168,"設定成交量下限");

var: BS(0),i(0),MKP(0),Rec_Open(0),Rec_High(0),Rec_Low(0),Rec_Close(0),reduF(5),dbF(3);
var: B_Condition(false),S_Condition(false);
var: R_wintimes(0),R_losetimes(0);

Array: intrabarpersist RTime[7](0),RDate[7](0),RPrice[7](0);
Array: intrabarpersist Max_Profit[7](0);	{ 啟動停利後最大獲利點 }
var: intrabarpersist T_RecTime(0),T_idx(0),T_i(0);


Condition1 = C[1] >EMA(C[1],6) and C[1] >EMA(C[1],20) and Filled=0 and Position=0 and AbsValue(TimeDiff(CurrentTime,T_RecTime,"M"))>TDiff;
Condition2 = C[1] <EMA(C[1],6) and C[1] <EMA(C[1],20) and Filled=0 and Position=0 and AbsValue(TimeDiff(CurrentTime,T_RecTime,"M"))>TDiff;

if Condition1 then begin
	SetPosition(Position + 1,Market,Label:="1st_多單");
	RTime[1]=currenttime;
	T_RecTime = MaxList(RTime[0],RTime[1],RTime[2],RTime[3],RTime[4],RTime[5],RTime[6]);
end;	

if Condition2 then begin
	SetPosition(Position - 1,Market,Label:="1st_空單");
	RTime[1]=currenttime;
	T_RecTime = MaxList(RTime[0],RTime[1],RTime[2],RTime[3],RTime[4],RTime[5],RTime[6]);
end;

//================================================================
If Filled > 0 then begin
	BS=1;
Print("up_R_losetimes = ",R_losetimes);

if loss_point > 0 and Close <= FilledAvgPrice - (loss_point/Filled) then begin		{ 多單停損 }
		R_losetimes += 1;
		Print("in_R_losetimes = ",R_losetimes);
		T_idx = 0;
						for i=1 to 7
						Begin
							Max_Profit[i] = 0;
							RPrice[i] = 0;
						End;
		RTime[6] = CurrentTime;		// 更新記錄_最後停損出場的時間
		T_RecTime = MaxList(RTime[0],RTime[1],RTime[2],RTime[3],RTime[4],RTime[5],RTime[6]);
		Alert("多單停損出場-RPrice[1]",RPrice[1],"RP[2]",RPrice[2],"RP[3]",RPrice[3],"RP[4]",RPrice[4],"RP[5]",RPrice[5],"RT[6]",RTime[6]);
		Alert("多單停損出場: C = ",Close,"F=",Filled,"成本",FilledAvgPrice,"多_停損價=",FilledAvgPrice - (loss_point/Filled));
		SetPosition(0,Market,Label:="多單停損出場");
		
end;

Print("CO_R_wintimes",R_wintimes,"down_R_losetimes = ",R_losetimes);

if FixedP > 0 and Close >= FilledAvgPrice + BS*FixedP then begin		
		RTime[0] = CurrentTime;		// 更新記錄_最後停利出場的時間
		T_RecTime = MaxList(RTime[0],RTime[1],RTime[2],RTime[3],RTime[4],RTime[5],RTime[6]);		
		SetPosition(0,Market,Label:="多單_固定停利");{ 停利 }
		R_wintimes += 1;
		Print("中_R_wintimes = ",R_wintimes);
end;

Print("後_R_wintimes = ",R_wintimes);
End;	// Filled > 0 的END

//================================================================

If Filled < 0 then begin
	BS=-1;
Print("前_R_losetimes = ",R_losetimes);

if loss_point > 0 and Close >= FilledAvgPrice - (loss_point/Filled) then begin		{ 空單停損 }
		R_losetimes += 1;
		Print("中_R_losetimes = ",R_losetimes);
		T_idx = 0;
						for i=1 to 7
						Begin
							Max_Profit[i] = 0;
							RPrice[i] = 0;
						End;
		RTime[6] = CurrentTime;		// 更新記錄_最後停損出場的時間
		T_RecTime = MaxList(RTime[0],RTime[1],RTime[2],RTime[3],RTime[4],RTime[5],RTime[6]);
		Alert("空單停損出場-RPrice[1]",RPrice[1],"RT[6]",RTime[6]);
		Alert("空單停損出場: C = ",Close,"F=",Filled,"成本",FilledAvgPrice,"空_停損價=",FilledAvgPrice - (loss_point/Filled));
		SetPosition(0,Market,Label:="空單停損出場");
		
end;

Print("後_R_losetimes = ",R_losetimes);
Print("前_R_wintimes",R_wintimes);

if FixedP > 0 and Close <= FilledAvgPrice + BS*FixedP then begin		
		RTime[0] = CurrentTime;		// 更新記錄_最後停利出場的時間
		T_RecTime = MaxList(RTime[0],RTime[1],RTime[2],RTime[3],RTime[4],RTime[5],RTime[6]);		
		SetPosition(0,Market,Label:="空單_固定停利");{ 停利 }
		R_wintimes += 1;
		Print("中_R_wintimes = ",R_wintimes);
end;

Print("後_R_wintimes = ",R_wintimes);
End;	// Filled < 0 的END

if Filled=0 then begin
	Print("R_wintimes = ",R_wintimes,"R_losetimes = ",R_losetimes);
	if R_wintimes >=1 and R_wintimes >= R_losetimes then begin
		RaiseRunTimeError("有賺錢就離開市場");
	end;
end;